﻿using FRMS.Helper;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.DAL
{
    public class MenuDAL
    {
        public bool AddMenu(MenuInfo menuInfo)
        {
            string strsql = string.Format("INSERT INTO menu_info(ParentID,MenuName,URL,Icon,projectID)VALUES({0},'{1}','{2}','{3}',{4})", menuInfo.ParentID, menuInfo.MenuName, menuInfo.URL, menuInfo.Icon, menuInfo.projectID);
            return SqlHelper<MenuInfo>.SqlExecute(strsql);
        }

        public bool EditMenu(MenuInfo menuInfo)
        {
            string strsql = string.Format("UPDATE menu_info SET ParentID={0},MenuName='{1}',URL='{2}',Icon='{3}',projectID={4} WHERE ID={5}", menuInfo.ParentID, menuInfo.MenuName, menuInfo.URL, menuInfo.Icon, menuInfo.projectID, menuInfo.ID);
            return SqlHelper<MenuInfo>.SqlExecute(strsql);
        }

        public bool DeleteMenu(MenuInfo menuInfo)
        {
            string strsql = "UPDATE menu_info SET IsDelete=1 WHERE ID=" + menuInfo.ID;
            return SqlHelper<MenuInfo>.SqlExecute(strsql);
        }

        public List<MenuInfo> QueryMenu(MenuInfo info)
        {
            string strsql = "SELECT * FROM menu_info";
            string sqlwhere = string.Empty;
            if (!string.IsNullOrEmpty(info.MenuName))
            {
                sqlwhere += " MenuName LIKE '%" + info.MenuName + "%' AND";
            }
          
            if (sqlwhere.Length > 0)
            {
                strsql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }
            if (!string.IsNullOrEmpty(info.Orderby))
            {
                strsql += " ORDER BY " + info.Orderby;
            }
            if (info.Recordhead > -1 && info.Recordtail > 0)
            {
                strsql += " LIMIT " + info.Recordhead + "," + info.Recordtail;
            }
            return SqlHelper<MenuInfo>.SqlList(strsql);
        }

        public int QueryMenuCount(MenuInfo info)
        {
            string strsql = "SELECT COUNT(1) FROM menu_info";
            string sqlwhere = string.Empty;
            if (!string.IsNullOrEmpty(info.MenuName))
            {
                sqlwhere += " MenuName LIKE '%" + info.MenuName + "%' AND";
            }
            
            if (sqlwhere.Length > 0)
            {
                strsql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }
            if (!string.IsNullOrEmpty(info.Orderby))
            {
                strsql += " ORDER BY " + info.Orderby;
            }
            return SqlHelper<MenuInfo>.SqlScalar(strsql);
        }

        public MenuInfo LoadMenu(MenuInfo info)
        {
            string strsql = "SELECT * FROM menu_info WHERE ID=" + info.ID;
            return SqlHelper<MenuInfo>.SqlSingleData(strsql);
        }

        public List<MenuList> GetParentMenu()
        {
            string strsql = "SELECT * FROM menu_info WHERE ParentID=0 AND IsDelete=0";
            return SqlHelper<MenuList>.SqlList(strsql);
        }
        public List<Menu> GetChildMenu(int parentid)
        {
            string strsql = "SELECT * FROM menu_info WHERE ParentID=" + parentid + " AND IsDelete=0";
            return SqlHelper<Menu>.SqlList(strsql);
        }
    }
}
